﻿//Table:S_EmailXX

#region Entity

using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using Entity;
using Platform.Configuration;
using Platform.Data;

namespace Entity
{
    /// <summary>
    ///     邮箱信息
    /// </summary>
    [Serializable]
    public class S_EmailXX
    {
        private DateTime? m_d_caozuosj;

        private int? m_i_leixing;

        private int? m_i_sid;

        private int? m_i_zhuangtai;
        private int m_id;

        private string m_s_buzhoubh;

        private string m_s_caozuor;

        private string m_s_emailadd;

        private string m_s_emailname;

        private string m_s_emailpws;

        private string m_s_remarks;

        private string m_s_smtpserveradd;

        /// <summary>
        ///     主键
        /// </summary>
        public int ID
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        ///     邮箱类型（1为发送2为接收）
        /// </summary>
        public int? I_LeiXing
        {
            get { return m_i_leixing; }
            set { m_i_leixing = value; }
        }

        /// <summary>
        ///     Email地址
        /// </summary>
        public string S_EmailAdd
        {
            get { return m_s_emailadd; }
            set { m_s_emailadd = value; }
        }

        /// <summary>
        ///     Email显示名
        /// </summary>
        public string S_EmailName
        {
            get { return m_s_emailname; }
            set { m_s_emailname = value; }
        }

        /// <summary>
        ///     Email密码
        /// </summary>
        public string S_EmailPWS
        {
            get { return m_s_emailpws; }
            set { m_s_emailpws = value; }
        }

        /// <summary>
        ///     Email SMTP服务器地址
        /// </summary>
        public string S_SMTPServerAdd
        {
            get { return m_s_smtpserveradd; }
            set { m_s_smtpserveradd = value; }
        }

        /// <summary>
        ///     操作人
        /// </summary>
        public string S_CaoZuoR
        {
            get { return m_s_caozuor; }
            set { m_s_caozuor = value; }
        }

        /// <summary>
        ///     操作时间
        /// </summary>
        public DateTime? D_CaoZuoSJ
        {
            get { return m_d_caozuosj; }
            set { m_d_caozuosj = value; }
        }

        /// <summary>
        ///     项目编号
        /// </summary>
        public int? I_SID
        {
            get { return m_i_sid; }
            set { m_i_sid = value; }
        }

        /// <summary>
        ///     步骤编号
        /// </summary>
        public string S_BuZhouBH
        {
            get { return m_s_buzhoubh; }
            set { m_s_buzhoubh = value; }
        }

        /// <summary>
        ///     备注
        /// </summary>
        public string S_Remarks
        {
            get { return m_s_remarks; }
            set { m_s_remarks = value; }
        }

        /// <summary>
        ///     状态（0是正常）
        /// </summary>
        public int? I_ZhuangTai
        {
            get { return m_i_zhuangtai; }
            set { m_i_zhuangtai = value; }
        }
    }
}

#endregion

#region EntityProcess

namespace BLL
{
    public abstract class S_EmailXXProcess
    {
        #region Field

        protected string conn;

        #endregion

        #region Construction

        public S_EmailXXProcess()
        {
            conn = ConfigHelper.PlatformConnString;
        }

        #endregion

        #region public

        public bool InsertOrUpdate(S_EmailXX S_EmailXX, DbTransaction Transaction)
        {
            if (S_EmailXX.ID == 0)
                InsertS_EmailXX(S_EmailXX, Transaction);
            else
                UpdateS_EmailXX(S_EmailXX, Transaction);
            return S_EmailXX.ID == 0 ? false : true;
        }

        public bool DeleteS_EmailXX(int ID, DbTransaction Transaction)
        {
            var spName = "_DeleteS_EmailXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public bool DeleteS_EmailXXesDynamic(string WhereClause, DbTransaction Transaction)
        {
            var spName = "_DeleteS_EmailXXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = WhereClause;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        public DataTable LeftJoin<EntityName>(string On, string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.Text,
                    "select * from S_EmailXX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.Text,
                    "select * from S_EmailXX left join " + typeof(EntityName).Name + " on " + On +
                    (string.IsNullOrEmpty(WhereClause.Trim()) ? "" : " where " + WhereClause) + " " +
                    (string.IsNullOrEmpty(OrderByExpression.Trim()) ? "" : " order by " + OrderByExpression));
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public S_EmailXX SelectS_EmailXX(int ID, DbTransaction Transaction)
        {
            var spName = "_SelectS_EmailXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            SqlDataReader rd = null;
            S_EmailXX _S_EmailXX = null;
            if (Transaction == null)
                rd = SqlHelper.ExecuteReader(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                rd = SqlHelper.ExecuteReader((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (rd.Read())
                _S_EmailXX = PopulateS_EmailXX(rd);
            if (rd != null)
                rd.Close();
            return _S_EmailXX;
        }

        public DataTable SelectS_EmailXXesAll(DbTransaction Transaction)
        {
            var spName = "_SelectS_EmailXXesAll";
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_EmailXXesDynamic(string WhereClause, string OrderByExpression,
            DbTransaction Transaction)
        {
            var spName = "_SelectS_EmailXXesDynamic";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = string.IsNullOrEmpty(WhereClause.Trim()) ? "1=1" : WhereClause;
            storedParams[1].Value = OrderByExpression;
            DataSet ds = null;
            if (Transaction == null)
                ds = SqlHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = SqlHelper.ExecuteDataset((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        public DataTable SelectS_EmailXXesBySerialNumberID(int SerialNumberID, string OrderByExpression,
            DbTransaction Transaction)
        {
            return SelectS_EmailXXesDynamic("SerialnumberID=" + SerialNumberID, OrderByExpression, Transaction);
        }

        #endregion

        #region Private

        private int InsertS_EmailXX(S_EmailXX S_EmailXX, DbTransaction Transaction)
        {
            var spName = "_InsertS_EmailXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_EmailXX.I_LeiXing;
            storedParams[1].Value = S_EmailXX.S_EmailAdd;
            storedParams[2].Value = S_EmailXX.S_EmailName;
            storedParams[3].Value = S_EmailXX.S_EmailPWS;
            storedParams[4].Value = S_EmailXX.S_SMTPServerAdd;
            storedParams[5].Value = S_EmailXX.S_CaoZuoR;
            storedParams[6].Value = S_EmailXX.D_CaoZuoSJ;
            storedParams[7].Value = S_EmailXX.I_SID;
            storedParams[8].Value = S_EmailXX.S_BuZhouBH;
            storedParams[9].Value = S_EmailXX.S_Remarks;
            storedParams[10].Value = S_EmailXX.I_ZhuangTai;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            S_EmailXX.ID = (int) storedParams[11].Value;
            return result;
        }

        private bool UpdateS_EmailXX(S_EmailXX S_EmailXX, DbTransaction Transaction)
        {
            var spName = "_UpdateS_EmailXX";
            var storedParams = SqlHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_EmailXX.ID;
            storedParams[1].Value = S_EmailXX.I_LeiXing;
            storedParams[2].Value = S_EmailXX.S_EmailAdd;
            storedParams[3].Value = S_EmailXX.S_EmailName;
            storedParams[4].Value = S_EmailXX.S_EmailPWS;
            storedParams[5].Value = S_EmailXX.S_SMTPServerAdd;
            storedParams[6].Value = S_EmailXX.S_CaoZuoR;
            storedParams[7].Value = S_EmailXX.D_CaoZuoSJ;
            storedParams[8].Value = S_EmailXX.I_SID;
            storedParams[9].Value = S_EmailXX.S_BuZhouBH;
            storedParams[10].Value = S_EmailXX.S_Remarks;
            storedParams[11].Value = S_EmailXX.I_ZhuangTai;
            var result = 0;
            if (Transaction == null)
                result = SqlHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = SqlHelper.ExecuteNonQuery((SqlTransaction) Transaction, CommandType.StoredProcedure, spName,
                    storedParams);
            return result == 0 ? false : true;
        }

        private S_EmailXX PopulateS_EmailXX(IDataReader dr)
        {
            var S_EmailXX = new S_EmailXX();
            S_EmailXX.ID = dr["ID"] == DBNull.Value ? 0 : (int) dr["ID"];
            S_EmailXX.I_LeiXing = dr["I_LeiXing"] == DBNull.Value ? null : (int?) dr["I_LeiXing"];
            S_EmailXX.S_EmailAdd = dr["S_EmailAdd"] == DBNull.Value ? null : (string) dr["S_EmailAdd"];
            S_EmailXX.S_EmailName = dr["S_EmailName"] == DBNull.Value ? null : (string) dr["S_EmailName"];
            S_EmailXX.S_EmailPWS = dr["S_EmailPWS"] == DBNull.Value ? null : (string) dr["S_EmailPWS"];
            S_EmailXX.S_SMTPServerAdd = dr["S_SMTPServerAdd"] == DBNull.Value ? null : (string) dr["S_SMTPServerAdd"];
            S_EmailXX.S_CaoZuoR = dr["S_CaoZuoR"] == DBNull.Value ? null : (string) dr["S_CaoZuoR"];
            S_EmailXX.D_CaoZuoSJ = dr["D_CaoZuoSJ"] == DBNull.Value ? null : (DateTime?) dr["D_CaoZuoSJ"];
            S_EmailXX.I_SID = dr["I_SID"] == DBNull.Value ? null : (int?) dr["I_SID"];
            S_EmailXX.S_BuZhouBH = dr["S_BuZhouBH"] == DBNull.Value ? null : (string) dr["S_BuZhouBH"];
            S_EmailXX.S_Remarks = dr["S_Remarks"] == DBNull.Value ? null : (string) dr["S_Remarks"];
            S_EmailXX.I_ZhuangTai = dr["I_ZhuangTai"] == DBNull.Value ? null : (int?) dr["I_ZhuangTai"];
            return S_EmailXX;
        }

        private void AddRowNumber(DataTable dt)
        {
            dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
            for (var i = 0; i < dt.Rows.Count; i++)
                dt.Rows[i][dt.Columns.Count - 1] = i + 1;
        }

        #endregion
    }
}

#endregion